app.controller("userListController", function ($scope, $http,toaster) {
    var thArray = ["编号", "姓名", "邮箱", "昵称", "城市", "创建时间"];
    console.log(toaster);
    $scope.thArray = thArray;
    //得到所有用户
    var params = {};
    var query = [];
    params.queryConditon = query;
    params.pageNumber = 0;
    var selectDatas = {};

    $scope.optionValue = 1;
    $scope.listInfo = function (pageSize) {
        params.pageSize = pageSize
        $http({
            url: '/multiple/user',
            method: 'get',
            params: params
        }).then(function (res) {

            $scope.userDatas = res.data.list;
            $scope.selectAll = false;
            $scope.all = function (m) {
                for (var i = 0; i < $scope.userDatas.length; i++) {
                    m ? $scope.userDatas[i].state = true : $scope.userDatas[i].state = false;
                }
            };

            $scope.all($scope.selectAll);
        }, function () {

        });
    }

    $scope.listInfo(10);

    //shanchu
    $scope.apply = function () {
        var operation = $scope.optionValue;

        if(operation == 1){
            toaster.pop("warning","选择正确的执行操作")
            return;
        }

        var argStr = "";
        angular.forEach($scope.userDatas, function (data, index) {
            data.state ? argStr += data.id + "," : null;
        });
        var arg = argStr.substring(0, argStr.length - 1);


        console.log(operation);
        switch (operation){
            case "2":
                del();
                break;
            case "3":
                exp();
                break;
            case "4":
                $scope.listInfo(10)
                break;
            case "5":
                $scope.listInfo(15)
                break;
            case "6":
                $scope.listInfo(20)
                break;
        }
        //daochu
        function exp() {
           //
            if(arg.length==0){
                toaster.pop("warning","无效操作")
                return;
            }
            var exportArg = {};
            exportArg.id = arg;
            $http.post("/multiple/user/download",arg,{ responseType: "arraybuffer" }).success(function(response){
                console.log(response);
                var blob = new Blob([response], {type: "application/vnd.ms-excel"});
                var objectUrl = URL.createObjectURL(blob);
                var aForExcel = $("<a><span class='forExcel'>下载excel</span></a>").attr("href",objectUrl);
                $("body").append(aForExcel);
                $(".forExcel").click();
                aForExcel.remove();
            })

        }
        //shan chu
        function del() {
            if(arg.length==0){
                toaster.pop("warning","无效操作")
                return;
            }
            $http.delete("/multiple/user/" + arg).then(function (data) {
                var status = data.status;
                switch (status){
                    case 200:toaster.pop("success","删除成功");
                        $scope.listInfo(10);
                        break;
                }
            }, function (data) {
                console.log(data);
                var status = data.status;
                switch (status){
                    case 404:toaster.pop("error","删除失败");
                }
            });
        }


    }
});